package framework;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;


/**
 * 
 * @author Michael Deng
 * @author Howard Chung
 * 
 */
public abstract class Sprite {

    private Level myLevel;
    private Point myCenter;
    private Dimension mySize;
    private Image myImage;
    private String myName = "Sprite";

    /**
     * Important: you must call super.superInitialize() in this method
     * 
     * @param position location to initialize the sprite
     * @param l level class that created this sprite
     */
    public abstract void initialize (Point position, Level l);

    /**
     * user implemented method governing the sprites movement
     * during each update cycle
     */
    protected abstract void move ();

    /**
     * user implemented method governing the sprites actions
     * during each update cycle
     */
    protected abstract void action ();

    /**
     * method used to initialize the sprite object
     * 
     * @param pos initial position
     * @param imageFile the appearance
     * @param width width of the sprite
     * @param height height of the sprite
     * @param l level object responsible for creating this sprite
     */
    protected void superInitialize (Point pos, String imageFile, int width, int height, Level l) {
        myLevel = l;
        myCenter = pos;
        mySize = new Dimension(width, height);
        myImage = myLevel.getMyGame().loadImage(imageFile);
    }

    /**
     * Updates the movement and actions for this sprite.
     */
    protected void update () {
        move();
        action();
    }

    /**
     * paints the sprite
     * 
     * @param pen pen used to paint the sprite
     */
    protected void paint (Graphics2D pen) {
        pen.drawImage(myImage, getLeft(), getTop(), mySize.width, mySize.height, null);
    }

    /**
     * 
     * @return a rectangle object that represents the bounds of the sprite
     */
    public Rectangle getBounds () {
        return new Rectangle(getLeft(), getTop(), mySize.width, mySize.height);
    }

    /**
     * Returns shape's left-most coordinate.
     */
    protected int getLeft () {
        return myCenter.x - mySize.width / 2;
    }

    /**
     * Returns shape's top-most coordinate.
     */
    protected int getTop () {
        return myCenter.y - mySize.height / 2;
    }

    /**
     * Returns shape's right-most coordinate.
     */
    protected int getRight () {
        return myCenter.x + mySize.width / 2;
    }

    /**
     * Reports shape's bottom-most coordinate.
     * 
     * @return bottom-most coordinate
     */
    protected int getBottom () {
        return myCenter.y + mySize.height / 2;
    }

    protected Level getMyLevel () {
        return myLevel;
    }

    /**
     * For others to see where this sprite is located
     * 
     * @return returns the center of this sprite
     */
    public Point getMyCenter () {
        return myCenter;
    }

    /**
     * Sets the myCenter variable
     * @param p point representing the new center
     */
    public void setMyCenter (Point p) {
        myCenter = p;
    }

    /**
     * Returns the string myName
     * @return String matching the name of the sprite
     */
    public String getMyName () {
        return myName;
    }

    /**
     * Sets the string myName
     * @param s string matching the new name of the sprite
     */
    protected void setMyName (String s) {
        myName = s;
    }
}
